#include <stc8g.h>
#include "intrins.h"
#include "pwm.h"
#include "uart.h"
#include "temperature.h"

void Delay1000ms()		//@11.0592MHz
{
	unsigned char i, j, k;

	_nop_();
	_nop_();
	i = 43;
	j = 6;
	k = 203;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}


void Delay4500ms()		//@11.0592MHz
{
	unsigned char i, j, k;

	_nop_();
	_nop_();
	i = 253;
	j = 120;
	k = 4;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}

void gpio_lint()
{
	P0M0 = 0x00;
	P0M1 = 0x00;
	P1M0 = 0x00;
	P1M1 = 0x00;
	P2M0 = 0x00;
	P2M1 = 0x00;
	P3M0 = 0x00;
	P3M1 = 0x00;
	P4M0 = 0x00;
	P4M1 = 0x00;
	P5M0 = 0x00;
	P5M1 = 0x00;
}

void main()
{
	int zkb = 0;
	int temp, humi, state, loc_temp;
	char end[] = 'e';
	gpio_lint();
	UartInit();
	ES = 1;
	EA = 1;
	pwm_init();
	CCAP0L = 0x3F; //PWM
	CCAP0H = 0x3F;
	loc_temp = 0;
	WDT_CONTR = 0x27;
	while (1)
	{
		WDT_CONTR = 0x37;
		if(zkb_buff[2] == 'e')
		{
			zkb = (zkb_buff[0] -48) *10 + (zkb_buff[1] -48);//这是一个占低比
			zkb = zkb *64 / 100;
			Delay1000ms();
			if(zkb >= 0x3F)
			{
				CCAP0L = 0x3F;
				CCAP0H = 0X3F;
			}else
			{
				CCAP0L = zkb;
				CCAP0H = zkb;
			}
		}
		state = dht11_read_data(&humi,&temp);
		if(state == 1)
		{
			UartSendInt(temp);
			UartSendStr(end);
		}
		Delay4500ms();
	}
}